<template>
	<div class="system-role-container layout-padding">
    <SearchForm :listQuery="state.listQuery" :formOption="state.formOption" @onSearch="onSearch" @resetSearch="resetSearch"></SearchForm>
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-button size="default" type="primary" @click="onOpenAddRole('add')">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增
				</el-button>
			</div>
			<el-table :data="state.tableData" v-loading="state.loading" border>
				<el-table-column type="index" label="序号" width="80" align="center" fixed="left"> </el-table-column>
				<el-table-column prop="name" label="名称" fixed="left" align="center">
					<template #default="scope">
						<el-button size="default" type="primary" link @click="onOpenViewRole('view', scope.row.id)">
							{{ scope.row.name }}
						</el-button>
					</template>
				</el-table-column>
        <el-table-column prop="code" label="编号" align="center">
				</el-table-column>
				<el-table-column prop="status" label="状态" align="center">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 'normal'">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
        <el-table-column prop="remark" label="描述" align="center"></el-table-column>
        <el-table-column prop="sort" label="排序" align="center"></el-table-column>
				<el-table-column prop="created_at" label="创建时间" align="center"></el-table-column>
				<el-table-column prop="updated_at" label="更新时间" align="center"></el-table-column>
				<el-table-column label="操作" width="200" fixed="right">
					<template #default="scope">
						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpenEditRole('edit', scope.row.id)"
							>修改</el-button
						>
						<el-button
							v-show="scope.row.status === 'disable'"
							:disabled="scope.row.roleName === '超级管理员'"
							size="small"
							text
							type="primary"
							@click="operate('normal', scope.row, '确认启用当前角色吗？')"
							>启用</el-button
						>
						<el-button
							v-show="scope.row.status === 'normal'"
							:disabled="scope.row.roleName === '超级管理员'"
							size="small"
							text
							type="primary"
							@click="operate('disable', scope.row, '确认禁用当前角色吗？')"
							>禁用</el-button
						>
						<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.listQuery.page"
				background
				v-model:page-size="state.listQuery.size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.totalCount"
			>
			</el-pagination>
		</div>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()"/>
	</div>
</template>

<script setup lang="ts" name="roleList">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { roleList, updateRole, deleteRole } from '/@/api/role';
import hotIcon from '/@/assets/hot-icon.svg';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/role/dialog.vue'));
// 搜索组件
const SearchForm = defineAsyncComponent(() => import('/@/components/searchForm/index.vue'));

// 定义变量内容
const roleDialogRef = ref();
const state = reactive({
	listQuery: {
		page: 1,
		size: 10,
		name: '',
		status: '',
	},
	clearForm: {
		page: 1,
		size: 10,
		name: '',
		status: '',
	},
	formOption: [
		{
			label: '名称',
			prop: 'name',
			element: 'input',
			option: {
				maxlength: 50,
			},
		},
		{
			label: '状态',
			prop: 'status',
			element: 'select',
			selectOption: [
				{ label: '正常', value: 'normal' },
				{ label: '已下架', value: 'disable' },
			],
		},
	],
	tableData: [],
	totalCount: 0,
	loading: false,
});

// 初始化信息
const initData = () => {};

// 初始化表格数据
const getTableData = () => {
	state.loading = true;
	roleList(state.listQuery).then((res: any) => {
		if (res.status === 'success') {
			state.tableData = res.data.data;
			state.totalCount = res.data.total;
		}
	});
	setTimeout(() => {
		state.loading = false;
	}, 500);
};
// 打开查看弹窗
const onOpenViewRole = (type: string, id: number) => {
	roleDialogRef.value.openDialog(type, id);
};
// 打开新增弹窗
const onOpenAddRole = (type: string) => {
	roleDialogRef.value.openDialog(type);
};
// 打开修改弹窗
const onOpenEditRole = (type: string, id: number) => {
	roleDialogRef.value.openDialog(type, id);
};
// 删除
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			deleteRole(row.id).then((res: any) => {
				if (res.status === 'success') {
					ElMessage.success('删除成功');
					getTableData();
				} else {
					ElMessage.error(res.msg);
				}
			});
		})
		.catch(() => {});
};
// 分页数量改变
const onHandleSizeChange = (val: number) => {
	state.listQuery.size = val;
	getTableData();
};
// 当前页改变
const onHandleCurrentChange = (val: number) => {
	state.listQuery.page = val;
	getTableData();
};
// 搜索
const onSearch = () => {
	getTableData();
};

// 重置
const resetSearch = () => {
	state.listQuery = JSON.parse(JSON.stringify(state.clearForm));
	getTableData();
};
// 启用、禁用、设为热门、取消热门
const operate = (type: string, row: any, msg: string) => {
	ElMessageBox.confirm(msg, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
	})
		.then(() => {
			let data = {};
			if (type === 'normal') {
				data = { id: row.id, status: 'normal' };
			} else if (type === 'disable') {
				data = { id: row.id, status: 'disable' };
			} 
			updateRole(data).then((res: any) => {
				if (res.status === 'success') {
					ElMessage.success('操作成功');
					getTableData();
				} else {
					ElMessage.error(res.msg);
				}
			});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	initData();
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}

	.hot-icon {
		position: absolute;
		width: 24px;
		height: 24px;
		right: 25px;
		top: 2px;
	}
}
</style>
